package graph;

import list.Queue;

/**
 * description:
 * author:张腾
 * date:2021-04-13
 */

/**
 * 有向图
 */
public class DiGraph {
    //顶点数目
    private final int V;
    //边数目
    private int E;
    //邻接表
    private Queue<Integer>[] adj;

    public DiGraph(int V){
        this.V = V;
        this.E = 0;
        this.adj = new Queue[V];

        for (int i = 0; i < adj.length; i++) {
            adj[i] = new Queue<Integer>();
        }
    }

    //获取顶点数目
    public int V(){
        return V;
    }

    //获取边数目
    public int E(){
        return E;
    }

    //向有向图中添加一条边 v->w
    public void addEdge(int v,int w){
        adj[v].enqueue(w);
        E++;
    }

    //获取由v指出的边所连接的所有顶点
    public Queue<Integer> adj(int v){

        return adj[v];
    }

    //获取该图的反向图
    private DiGraph reverse(){

        DiGraph diGraph = new DiGraph(V);
        for (int v = 0; v < V; v++) {
            //获取由v指出的所有边
            for (Integer w : adj[v]) {
                diGraph.addEdge(w,v);
            }
        }

        return diGraph;
    }
}
